package com.groupbuying.provider.service.impl;

import com.groupbuying.provider.dao.StoreMenuDao;
import com.groupbuying.provider.dao.StoreRoleMenuDao;
import com.groupbuying.provider.dto.RouterDTO;
import com.groupbuying.provider.dto.forManage.StoreMenuDO;
import com.groupbuying.provider.dto.forManage.Tree;
import com.groupbuying.provider.service.StoreMenuService;
import com.groupbuying.provider.utils.BuildTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StoreMenuServiceImpl implements StoreMenuService {
    @Autowired
    StoreMenuDao menuMapper;
    @Autowired
    StoreRoleMenuDao roleMenuMapper;


    @Override
    public Tree<StoreMenuDO> getTree() {
        List<Tree<StoreMenuDO>> trees = new ArrayList<Tree<StoreMenuDO>>();
        List<StoreMenuDO> menuDOs = menuMapper.list(new HashMap<>(16));
        for (StoreMenuDO menuDO : menuDOs) {
            Tree<StoreMenuDO> tree = new Tree<StoreMenuDO>();
            tree.setId(menuDO.getMenuId().toString());
            tree.setParentId(menuDO.getParentId().toString());
            tree.setText(menuDO.getName());
            tree.setObject(menuDO);

            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<StoreMenuDO> t = BuildTree.build(trees);
        return t;
    }

    @Override
    public StoreMenuDO get(String id) {
        StoreMenuDO menuDO = menuMapper.get(id);
        return menuDO;
    }

    @Override
    public List<StoreMenuDO> list(Map<String, Object> params) {
        List<StoreMenuDO> menus = menuMapper.list(params);
        return menus;
    }

    @Override
    public List<StoreMenuDO> userMenus(String userId) {
        return menuMapper.listMenuByAccountId(userId);
    }

    @Override
    public List<Tree<StoreMenuDO>> listMenuTree(String id) {
        List<Tree<StoreMenuDO>> trees = new ArrayList<Tree<StoreMenuDO>>();
        List<StoreMenuDO> menuDOs = menuMapper.listMenuByAccountId(id);
        for (StoreMenuDO menuDO : menuDOs) {
            Tree<StoreMenuDO> tree = new Tree<StoreMenuDO>();
            tree.setId(menuDO.getMenuId().toString());
            tree.setParentId(menuDO.getParentId().toString());
            tree.setText(menuDO.getName());
            Map<String, Object> attributes = new HashMap<>(16);
            attributes.put("url", menuDO.getUrl());
            attributes.put("icon", menuDO.getIcon());
            tree.setAttributes(attributes);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        List<Tree<StoreMenuDO>> list = BuildTree.buildList(trees, "0");
        return list;
    }

    @Override
    public List<BigInteger> MenuIdsByRoleId(String roleId) {
        // 根据roleId查询权限,只保留子节点，父节点的选中或半选中状态，前台自动调整
        List<StoreMenuDO> menus = menuMapper.list(new HashMap<String, Object>(16));
        List<BigInteger> menuIds = roleMenuMapper.listMenuIdByRoleId(roleId);
        List<BigInteger> temp = menuIds;
        for (StoreMenuDO menu : menus) {
            if (temp.contains(menu.getParentId())) {
                menuIds.remove(menu.getParentId());
            }
        }
        return menuIds;
    }

    //用户登录
    @Override
    public List<String> PermsByUserId(String userId) {
        List<String> permsList = new ArrayList<>();
        List<StoreMenuDO> menuDOs = userMenus(userId);
        for (StoreMenuDO menuDO : menuDOs) {
            if (menuDO.getPerms() != null && "" != menuDO.getPerms()) {
                permsList.add(menuDO.getPerms());
            }
        }
        return permsList;
    }

    /**
     * 用户的路由
     *
     * @return
     */
    @Override
    public List<RouterDTO> RouterDTOsByUserId(String userId) {
        List<StoreMenuDO> menuDOs = userMenus(userId);
        List<RouterDTO> routerDTOs = new ArrayList<>();
        for (StoreMenuDO menuDO : menuDOs) {
            RouterDTO routerDTO = new RouterDTO();
            routerDTO.setId(menuDO.getMenuId().toString());
            routerDTO.setParentId(menuDO.getParentId().toString());
            routerDTO.setPath(menuDO.getUrl());
            routerDTO.setComponent(menuDO.getComponent());
            routerDTO.setName(menuDO.getName());
            routerDTO.setIconCls(menuDO.getIcon());
            routerDTO.setMenuShow(true);
            routerDTO.setChildren(new ArrayList<>());
            routerDTO.setLeaf(null != menuDO.getType() && menuDO.getType() == 1);
            routerDTOs.add(routerDTO);
        }
        return RouterDTO.buildList(routerDTOs, 0L);
    }

}
